<?php
/**
 * Created by PhpStorm.
 * User: aron
 * Date: 2019/5/7
 * Time: 1:54 PM
 */


include_once "../../Util/DebugUtil/DebugUtil.php";

$lamp = array(
    "l" => "Linux",
    "a" => "Apache",
    "m" => "Mysql",
    "p" => "PHP",
);

// count 函数计算数组长度
DebugUtil::printDebugSeperator();
$numberOfLamp = count($lamp);
echo "count(\$lamp) = $numberOfLamp";

// array_count_values 函数统计value出现的次数
DebugUtil::printDebugSeperator();
$arrayCountValues = array_count_values($lamp);
echo "array_count_values(\$lamp) = $arrayCountValues";
print_r($arrayCountValues);
// Output:
//array_count_values($lamp) = ArrayArray
//(
//    [Linux] => 1
//    [Apache] => 1
//    [Mysql] => 1
//    [PHP] => 1
//)


// array_unique 函数用于删除数组中的重复元素
DebugUtil::printDebugSeperator();

$lamp = array(
    "l" => "Linux",
    "a" => "Apache",
    "m" => "Mysql",
    "p" => "PHP",
    "h" => "PHP",
);

print_r($lamp);
$uniqued_array = array_unique($lamp);
print_r($uniqued_array);
// Output:
//Array
//(
//    [Linux] => 1
//    [Apache] => 1
//    [Mysql] => 1
//    [PHP] => 1
//)


// array_filter 函数用于筛选数组中的指定元素
DebugUtil::printDebugSeperator();

function phpFilter($value) {
    return $value == "PHP";
}

$filteredArray = array_filter($lamp, phpFilter);
print_r($filteredArray);
// Output:
//Array
//(
//    [p] => PHP
//    [h] => PHP
//)


// array_walk 函数
DebugUtil::printDebugSeperator();

function walkFunWithOneParam($value) {
    echo "=> $value \r\n";
}

array_walk($lamp, walkFunWithOneParam);
// Output:
//=> Linux
//=> Apache
//=> Mysql
//=> PHP
//=> PHP


function walkFunWithTwoParam($value, $key) {
    echo "$key => $value \r\n";
}
array_walk($lamp, walkFunWithTwoParam);
// Output:
//l => Linux
//a => Apache
//m => Mysql
//p => PHP
//h => PHP


// array_map 函数
DebugUtil::printDebugSeperator();

function mapFun($value) {
    if ($value == "Apache") {
        return "SqlServer";
    }
    return $value;
}
$mapedArr = array_map(mapFun, $lamp);
print_r($mapedArr);
// Output:
//Array
//(
//    [l] => Linux
//    [a] => SqlServer
//    [m] => Mysql
//    [p] => PHP
//    [h] => PHP
//)


echo "END";

